package logic

import (
	"context"
	"errors"
	"house/models"

	"house/houserpc/houserpc"
	"house/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectDelLogic {
	return &CollectDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectDelLogic) CollectDel(in *houserpc.CollectDelRequest) (*houserpc.CollectDelResponse, error) {
	// todo: add your logic here and delete this line
	var collect models.Collect
	tx := models.DB.Begin()
	tx.Where("id=?", in.CollectId).Find(&collect)
	if collect.ID == 0 {
		return nil, errors.New("没有找到此收藏")
	}
	tx.Delete(&models.Collect{}, in.CollectId)
	var house models.House
	house.CollectCount = house.CollectCount - 1
	tx.Updates(house)
	return &houserpc.CollectDelResponse{}, nil
}
